{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**pyecharts 库的基本使用用法**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 安装pyecharts  \n",
    "pip install pyecharts  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 开始使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyecharts import Bar\n",
    "from pyecharts import Bar\n",
    "\n",
    "df = pd.read_excel(r\"./Pyecharts.xlsx\")\n",
    "brands = df['品牌'].values\n",
    "solds = df['已售'].values\n",
    "bar = Bar(\"汽车各品牌销量\", \"这里是测试数据\")\n",
    "bar.add(\"销量\", brands, solds)\n",
    "# bar.print_echarts_options() # 该行只为了打印配置项，方便调试时使用\n",
    "bar.render(\"./html/start.html\")    # 生成本地 HTML 文件"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![图片](html/images/start.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- add()：主要方法，用于添加图表的数据和设置各种配置项\n",
    "- print_echarts_options()：打印输出图表的所有配置项\n",
    "- render()：默认将会在根目录下生成一个 render.html 的文件，支持 path 参数，设置文件保存位置，如 render(r\"e:\\my_first_chart.html\")，文件用浏览器打开。  \n",
    "**Note：**可以按右边的下载按钮将图片下载到本地，如果想要提供更多实用工具按钮，请在 add() 中设置 is_more_utils 为 True"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 使用主题"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyecharts import Bar\n",
    "from pyecharts import Bar\n",
    "\n",
    "df = pd.read_excel(r\"./Pyecharts.xlsx\")\n",
    "brands = df['品牌'].values\n",
    "solds = df['已售'].values\n",
    "bar = Bar(\"汽车各品牌销量\", \"这里是测试数据\")\n",
    "bar.use_theme('dark')\n",
    "bar.add(\"销量\", brands, solds)\n",
    "bar.render(\"./html/dark.html\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![图片](html/images/dark.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 使用 pyecharts-snapshot 插件  \n",
    "如果想直接将图片保存为 png, pdf, gif 格式的文件，可以使用 pyecharts-snapshot。使用该插件请确保你的系统上已经安装了 Nodejs 环境。  \n",
    "- 安装 phantomjs  \\$ npm install -g phantomjs-prebuilt<br>  \n",
    "- 安装 pyecharts-snapshot  $ pip install pyecharts-snapshot  \n",
    "- 调用 render 方法  bar.render(path='snapshot.png') 文件结尾可以为 svg/jpeg/png/pdf/gif。请注意，svg 文件需要你在初始化 bar 的时候设置 renderer='svg'。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 图形绘制过程\n",
    "- 实例一个具体类型图表的对象 chart = FooChart()\n",
    "- 为图表添加通用的配置，如主题 chart.use_theme()\n",
    "- 为图表添加特定的配置 geo.add_coordinate()\n",
    "- 添加数据及配置项 chart.add()\n",
    "- 生成本地文件（html/svg/jpeg/png/pdf/gif） chart.render()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 基本图表"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Bar（柱状图/条形图）\n",
    ">柱状/条形图，通过柱形的高度/条形的宽度来表现数据的大小。  \n",
    "\n",
    "Bar.add() 方法签名  \n",
    "```python\n",
    "add(name, x_axis, y_axis,\n",
    "    is_stack=False,\n",
    "    bar_category_gap='20%', **kwargs)\n",
    "```  \n",
    "- name -> str  \n",
    "图例名称\n",
    "- attr -> list  \n",
    "属性名称\n",
    "- value -> list  \n",
    "属性所对应的值\n",
    "- shape -> list  \n",
    "词云图轮廓，有'circle', 'cardioid', 'diamond', 'triangle-forward', 'triangle', 'pentagon', 'star'可选\n",
    "- word_gap -> int  \n",
    "单词间隔，默认为 20。\n",
    "- word_size_range -> list  \n",
    "单词字体大小范围，默认为 [12, 60]。\n",
    "- rotate_step -> int  \n",
    "旋转单词角度，默认为 45"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from pyecharts import Bar\n",
    "\n",
    "df = pd.read_excel(r\"./Pyecharts.xlsx\")\n",
    "\n",
    "brands = df['品牌'].values\n",
    "solds = df['已售'].values\n",
    "schedules = df['已预订'].values\n",
    "bar = Bar(\"汽车各品牌销量\")\n",
    "bar.add(\"已售\", brands, sold, is_stack=True)\n",
    "bar.add(\"已预订\", brands, schedules, is_stack=True)\n",
    "bar.render(\"./html/bar01.html\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![图片](html/images/bar.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Pie（饼图）\n",
    ">饼图主要用于表现不同类目的数据在总和中的占比。每个的弧度表示数据数量的比例。  \n",
    "\n",
    "Pie.add() 方法签名\n",
    "```python\n",
    "add(name, attr, value,\n",
    "    radius=None,\n",
    "    center=None,\n",
    "    rosetype=None, **kwargs)\n",
    "```    \n",
    "- name -> str   \n",
    "图例名称\n",
    "- attr -> list  \n",
    "属性名称\n",
    "- value -> list   \n",
    "属性所对应的值\n",
    "- radius -> list  \n",
    "饼图的半径，数组的第一项是内半径，第二项是外半径，默认为 [0, 75]   \n",
    "默认设置成百分比，相对于容器高宽中较小的一项的一半\n",
    "- center -> list   \n",
    "饼图的中心（圆心）坐标，数组的第一项是横坐标，第二项是纵坐标，默认为 [50, 50]   \n",
    "默认设置成百分比，设置成百分比时第一项是相对于容器宽度，第二项是相对于容器高度\n",
    "- rosetype -> str  \n",
    "是否展示成南丁格尔图，通过半径区分数据大小，有'radius'和'area'两种模式。默认为'radius'  \n",
    "radius：扇区圆心角展现数据的百分比，半径展现数据的大小  \n",
    "area：所有扇区圆心角相同，仅通过半径展现数据大小"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyecharts import Pie\n",
    "import pandas as pd\n",
    "df = pd.read_excel(r\"./Pyecharts.xlsx\")\n",
    "brands = df[\"品牌\"].values\n",
    "Sales = df[\"总计\"].values\n",
    "pie = Pie(\"汽车各品牌销量\")\n",
    "pie.add(\"\", brands , Sales, is_label_show=True)\n",
    "pie.render(\"./html/pie.html\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![pie](./html/images/pie.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### WordCloud（词云图）  \n",
    "WordCloud.add() 方法签名  \n",
    "```python\n",
    "add(name, attr, value,\n",
    "    shape=\"circle\",\n",
    "    word_gap=20,\n",
    "    word_size_range=None,\n",
    "    rotate_step=45)\n",
    "```\n",
    "- name -> str  \n",
    "图例名称\n",
    "- attr -> list  \n",
    "属性名称\n",
    "- value -> list  \n",
    "属性所对应的值\n",
    "- shape -> list  \n",
    "词云图轮廓，有'circle', 'cardioid', 'diamond', 'triangle-forward', 'triangle', 'pentagon', 'star'可选\n",
    "- word_gap -> int  \n",
    "单词间隔，默认为 20。\n",
    "- word_size_range -> list  \n",
    "单词字体大小范围，默认为 [12, 60]。\n",
    "- rotate_step -> int  \n",
    "旋转单词角度，默认为 45"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyecharts import WordCloud\n",
    "import pandas as pd\n",
    "df = pd.read_excel(r\"./Pyecharts.xlsx\",sheet_name=1)\n",
    "brands = df[\"品牌\"].values\n",
    "sales = df[\"总计\"].values\n",
    "wordcloud = WordCloud(width=1300, height=620)\n",
    "wordcloud.add(\"\", brands, sales, word_size_range=[20, 100])\n",
    "wordcloud.render(\"./html/WordCloud.html\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![WordCloud](./html/images/WordCloud.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Gauge（仪表盘）  \n",
    "Gauge.add() 方法签名  \n",
    "```python\n",
    "add(name, attr, value,\n",
    "    scale_range=None,\n",
    "    angle_range=None, **kwargs)\n",
    "```\n",
    "- name -> str  \n",
    "图例名称\n",
    "- attr -> list  \n",
    "属性名称\n",
    "- value -> list  \n",
    "属性所对应的值  \n",
    "- scale_range -> list  \n",
    "仪表盘数据范围。默认为 [0, 100]\n",
    "- angle_range -> list  \n",
    "仪表盘角度范围。默认为 [225, -45]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyecharts import Gauge\n",
    "\n",
    "gauge = Gauge(\"仪表盘示例\")\n",
    "gauge.add(\"业务指标\", \"完成率\", 66.66)\n",
    "gauge.render(\"./html/Gauge01.html\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![Gauge01](./html/images/Gauge01.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [],
   "source": [
    "gauge = Gauge(\"仪表盘示例\")\n",
    "gauge.add(\n",
    "    \"业务指标\",\n",
    "    \"完成率\",\n",
    "    166.66,\n",
    "    angle_range=[180, 0],\n",
    "    scale_range=[0, 200],\n",
    "    is_legend_show=False,\n",
    ")\n",
    "gauge.render(\"./html/Gauge02.html\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![Gauge02](./html/images/Gauge02.png)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
